package net.masker.bbtweet;

import net.rim.blackberry.api.homescreen.HomeScreen;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import net.rim.device.api.io.Base64OutputStream;

//bbtweet configuration properties. Kept in persistent store.
public class bbtweetProperties implements Persistable
{
    private boolean bbtweetEnabled;
	private String username;
	private String password;
	private String authenticationHash;
    private int updateInterval;
    private int lastID;
    private String bbtweetStatusMessage;
    private boolean connectionManagerStarted;
    private String statusUpdate;
    private boolean pushStatusUpdate;

    //Hash of net.masker.bbtweet.bbtweetProperties.PERSISTENCE_ID 
    private static final long PERSISTENCE_ID = 0xa3388102e219a8a9L;

    //Persistent object wrapping the effective properties instance
    private static PersistentObject store;

    //Ensure that an effective properties set exists on startup.
    static {
        store = PersistentStore.getPersistentObject(PERSISTENCE_ID);
        synchronized (store) {
            if (store.getContents() == null) {
                store.setContents(new bbtweetProperties());
                store.commit();
            }
        }
    }

    // Constructs a properties set with default values.
    private bbtweetProperties()
    {
        bbtweetEnabled = false;
        username = "";
        password = "";
        authenticationHash = null;
        updateInterval = 15;
        lastID = -1;
        bbtweetStatusMessage = null;
        connectionManagerStarted = false;
        statusUpdate = "";
        pushStatusUpdate = false;
    }        

    //Retrieves a copy of the effective properties set from storage.
    public static bbtweetProperties fetch()
    {
        bbtweetProperties savedProps =
          (bbtweetProperties) store.getContents();
        return new bbtweetProperties(savedProps);
    }

    //Causes the values within this instance to become the effective
    //properties for the application by saving this instance to the store.
    public void save()
    {
        // update hash
    	if ((username.length() > 0) && (password.length() > 0))
    	{
    		byte[] toEncode = (username + ":" + password).getBytes();
    		ByteArrayOutputStream newHash = new ByteArrayOutputStream(toEncode.length);
    		Base64OutputStream base64OutputStream = new Base64OutputStream(newHash);
    		
    		try
    		{
    			base64OutputStream.write(toEncode, 0, toEncode.length);
    			base64OutputStream.flush();
    			base64OutputStream.close();
    		}
    		catch (IOException ioe)
    		{
    			//Don't save garbage
    			newHash = null;
    		}
    		
    		// Set hash and clear out password
    		authenticationHash = newHash.toString();
    		password = "";
    	}
    	// If username is null, hash should also be null
    	else if ("".equals(username))
    	{
    		authenticationHash = null;
    	}
    	
    	store.setContents(this);
        store.commit();
        
        
    }

    //Method for retrieving bbtweet status
    public boolean isBbtweetEnabled()
    {
    	return bbtweetEnabled;
    }
    
    //Method for retrieving username
    public String getUsername()
    {
    	return username;
    }
    
    /* No need to retrieve the password
    //Method for retrieving password
    public String getPassword()
    {
    	return password;
    }
    */
    
    //Method for retrieving authentication hash
    public String getAuthenticationHash()
    {
    	return authenticationHash;
    }
    
    //Method for retrieving updateInterval
    public int getUpdateInterval()
    {
    	return updateInterval;
    }
    
    //Method for retrieving lastID
    public int getLastID()
    {
    	return lastID;
    }
    
    //Method for retrieving statusMessage
    public String getBbtweetStatusMessage()
    {
    	return bbtweetStatusMessage;
    }
    
    //Method for retrieving connection manager status
    public boolean isConnectionManagerStarted()
    {
    	return connectionManagerStarted;
    }
    
    //Method for retrieving saved status update
    public String getStatusUpdate()
    {
    	return statusUpdate;
    }
    
    //Method for retrieving pushStatusUpdate state
    public boolean isPushStatusUpdate()
    {
    	return pushStatusUpdate;
    }

    public void setBbtweetEnabled(boolean bbtweetEnabledLocal)
    {
    	bbtweetEnabled = bbtweetEnabledLocal;
    }
    
    public void setUsername(String usernameLocal)
    {
    	username = usernameLocal;
    }
    
    public void setPassword(String passwordLocal)
    {
    	password = passwordLocal;
    }
    
    public void setUpdateInterval(int updateIntervalLocal)
    {
    	updateInterval = updateIntervalLocal;
    }
    
    public void setLastID(int lastIDLocal)
    {
    	lastID = lastIDLocal;
    }
    
    public void setBbtweetStatusMessage(String statusMessageLocal)
    {
    	bbtweetStatusMessage = statusMessageLocal;
    }
    
    public void setConnectionManagerStarted(boolean connectionManagerStartedLocal)
    {
    	connectionManagerStarted = connectionManagerStartedLocal;
    }
    
    public void setStatusUpdate(String statusUpdateLocal)
    {
    	statusUpdate = statusUpdateLocal;
    }
    
    public void setPushStatusUpdate(boolean pushStatusUpdateLocal)
    {
    	pushStatusUpdate = pushStatusUpdateLocal;
    }
    

    //Cannonical copy constructor.
    private bbtweetProperties(bbtweetProperties other)
    {
        bbtweetEnabled = other.bbtweetEnabled;
        username = other.username;
        password = other.password;
        authenticationHash = other.authenticationHash;
        updateInterval = other.updateInterval;
        lastID = other.lastID;
        bbtweetStatusMessage = other.bbtweetStatusMessage;
        connectionManagerStarted = other.connectionManagerStarted;
        statusUpdate = other.statusUpdate;
        pushStatusUpdate = other.pushStatusUpdate;
    }
}